// pages/addTlitl/addTlitl.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    x: false,
    xList: '',
    xval: '',
    id: '',
    list: '',
    clothingStyleList: '',
    index: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this
    if (options.id != '' && options.index == 'undefined') {
      this.setData({
        id: options.id,
        index: options.index,
        list: app.globalData.clothingStyleList[options.id],
        xval: app.globalData.clothingStyleList[options.id].styleNumber
      })
    } else if (options.id != '' &&options.index != 'undefined') {
      wx.request({
        url: app.globalData.api + 'businessOrderForm/' + options.id,
        data: {},
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success: (res) => {
          console.log(res)
          if (res.data.code == 200) {
            that.setData({
              list: res.data.data.clothingStyleList[options.index],
              xval: res.data.data.clothingStyleList[options.index].styleNumber,
              clothingStyleList: res.data.data.clothingStyleList,
              id: options.id,
              index: options.index
            })
          }
        }
      })
    }



  },
  formSubmit: function(e) {
    console.log(app.globalData.clothingStyleList[this.data.id], e)
    if (e.detail.value.styleNumber == '' || e.detail.value.styleColor == '' || e.detail.value.styleMeasure == '' || e.detail.value.preOrderQuantity == '' ){
      wx.showToast({
        title: '请填写完整信息',
      })
      return
    }
    if (this.data.id == '') {
      app.globalData.clothingStyleList.push({
        styleNumber: e.detail.value.styleNumber,
        styleColor: e.detail.value.styleColor,
        styleMeasure: e.detail.value.styleMeasure,
        preOrderQuantity: e.detail.value.preOrderQuantity,
        remarks: e.detail.value.remarks
      })
    } else if (this.data.id != '' && this.data.index == 'undefined') {
      app.globalData.clothingStyleList[this.data.id].styleNumber = e.detail.value.styleNumber
      app.globalData.clothingStyleList[this.data.id].styleColor = e.detail.value.styleColor
      app.globalData.clothingStyleList[this.data.id].styleMeasure = e.detail.value.styleMeasure
      app.globalData.clothingStyleList[this.data.id].preOrderQuantity = e.detail.value.preOrderQuantity
      app.globalData.clothingStyleList[this.data.id].remarks = e.detail.value.remarks
    } else if (this.data.id != '' && this.data.index != 'undefined'){
      this.data.clothingStyleList[this.data.index].styleNumber = e.detail.value.styleNumber
      this.data.clothingStyleList[this.data.index].styleColor = e.detail.value.styleColor
      this.data.clothingStyleList[this.data.index].styleMeasure = e.detail.value.styleMeasure
      this.data.clothingStyleList[this.data.index].preOrderQuantity = e.detail.value.preOrderQuantity
      this.data.clothingStyleList[this.data.index].remarks = e.detail.value.remarks
      wx.request({
        url: app.globalData.api + 'businessOrderForm/' + this.data.id,
        data: {
          clothingStyleList: this.data.clothingStyleList
        },
        header: {
          Authorization: wx.getStorageSync('token')
        },
        method:'PUT',
        success: (res) => {
          console.log(res)
        }
      })
    }



    wx.navigateBack({
      delta: 1
    })


  },
  search: function(e) {
    // console.log(e)
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/getDesignDiagramStyleNumber/' + e.detail.value,
      data: {

      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            x: true,
            xList: res.data.data
          })
        }
      }
    })
  },
  xz: function(e) {
    console.log(e)
    this.setData({
      xval: e.currentTarget.dataset.val,
      x: false
    })
  },
  offsearch: function() {
    setTimeout(() => {
      this.setData({
        x: false
      })
    }, 500)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})